﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using CE.User;
using CE.Global;
using CE.Provider;

public partial class Provider_CarInsurance_ProviderCarTurboEngine : System.Web.UI.Page
{
    string PageName = "CXCI";
    string SubPageName = "CXCITURENG";
    public static Int64 _providercarturboenineid = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[CE.Global.General.UserId] == null || Session[CE.Global.General.ProviderCompanyId] == null)
        {
            Response.Redirect("~/Provider/ProviderSecureLogin.aspx");
        }
        CheckMainPageAccess();
        CheckSubPageAccess();

        usrEffectivityDate.StartYear = CE.Global.General.GetUTCDateTime().Year;
        usrEffectivityDate.EndYear = CE.Global.General.GetUTCDateTime().Year + 2; 

        //lblMessage.Text = Boolean.TrueString + " " + bool.FalseString;
    }

    protected void ibtSubmit_Click(object sender, ImageClickEventArgs e)
    {
        if (Page.IsValid)
        {
            string _authcode = CE.Security.MySecurity.Encrypt(txtAuthCode.Text, true);
            if (CheckAuthCode(_authcode, 100))
            {
                CarTurboEngine _carturboengine = new CarTurboEngine();
                CarTurboEngineDB _carturboenginedb = new CarTurboEngineDB();
                int _trantype = 1;
                
                _carturboengine.ProviderCarTurboEngineId = CheckCarTurboEngine(Convert.ToInt64(Session[General.ProviderCompanyId].ToString()), Convert.ToBoolean(ddlTurbo.Text));

                if (_carturboengine.ProviderCarTurboEngineId > 0)
                {
                    _trantype = 2;
                }
                _carturboengine.ProviderCompanyId = Convert.ToInt32(Session[CE.Global.General.ProviderCompanyId].ToString());
                _carturboengine.IsTurbo = Convert.ToBoolean(ddlTurbo.Text);

                float _rate = 0.0f;
                if (float.TryParse(txtRate.Text, out _rate))
                {
                    _carturboengine.TurboEngineRate = _rate;
                }
                _carturboengine.Status = true;
                _carturboengine.RefCEUserId = Convert.ToInt64(Session[CE.Global.General.UserId].ToString());

                if (rdoEffectivity.SelectedValue == "Yes" && usrEffectivityDate.GetDate() > CE.Global.General.GetUTCDateTime())
                {
                    _carturboengine.FInfo1 = float.Parse(txtNewRate.Text);
                    _carturboengine.DInfo1 = usrEffectivityDate.GetDate();

                }
                else
                {
                    _carturboengine.FInfo1 = float.Parse(txtRate.Text);
                    _carturboengine.DInfo1 = CE.Global.General.GetUTCDateTime();

                }

                _carturboengine.ProviderCarTurboEngineId = _carturboenginedb.Insert(_carturboengine);

                if (_carturboengine.ProviderCarTurboEngineId > 0)
                {
                    InsertProviderAuthTransaction(_carturboengine.ProviderCarTurboEngineId, "CICarTurboEnginie", _trantype, 100, _authcode, _carturboengine.ProviderCompanyId, _carturboengine.RefCEUserId);
                    ProviderUpdateSave(_carturboengine.ProviderCarTurboEngineId);
                    lblMessage.Text = "Updated successfully!";
                }
                else
                {
                    lblMessage.Text = "Updating failed!";
                }
            }
            else
            {
                lblMessage.Text = "Wrong auth code!";
            }
        }
    }

    private void ProviderUpdateSave(Int64 ProviderRefId)
    {
        ProviderUpdates _providerupdates = new ProviderUpdates();
        ProviderUpdatesDB _providerupdatesDB = new ProviderUpdatesDB();

        _providerupdates.UpdateId = 0;
        _providerupdates.CXTableId = GetCXTableId();
        _providerupdates.ProviderRefId = ProviderRefId;

        if (rdoEffectivity.SelectedValue == "Yes" && usrEffectivityDate.GetDate() > CE.Global.General.GetUTCDateTime())
        {
            //Scheduled
            _providerupdates.UpdateType = 1;
        }
        else
        {
            //Immediate
            _providerupdates.UpdateType = 0;
        }

        _providerupdates.ProviderCompanyId = Convert.ToInt64(Session[CE.Global.General.ProviderCompanyId]);
        _providerupdates.RefCEUserID = Convert.ToInt64(Session[General.UserId].ToString());

        _providerupdatesDB.Insert(_providerupdates);

    }

    private Int32 GetCXTableId()
    {
        Int32 CXID = 0;

        ProviderTableListings _tablelist = new ProviderTableListings();
        ProviderTableListingsDB _tablelistdb = new ProviderTableListingsDB();

        int ProviderType = 100; //Car Insurance
        string CXTable = "tblProviderCarTurboEngine";

        _tablelist = _tablelistdb.GetProviderTableListingsSingleItem(ProviderType, Convert.ToInt64(Session[CE.Global.General.ProviderCompanyId]), CXTable);

        if (_tablelist != null)
            CXID = _tablelist.CXTableId;

        return CXID;

    }

    private void ShowCarTurboEngine(Int64 ProviderCompanyId, bool IsTurbo)
    {
        CarTurboEngine _carturboengine = new CarTurboEngine();
        CarTurboEngineDB _carturboenginedb = new CarTurboEngineDB();
        _carturboengine = _carturboenginedb.GetCarTurboEngine(ProviderCompanyId, IsTurbo);
        if (_carturboengine != null)
        {
            ddlTurbo.Text = _carturboengine.IsTurbo.ToString();
            txtRate.Text = _carturboengine.TurboEngineRate.ToString();

            if (_carturboengine.DInfo1 > CE.Global.General.GetUTCDateTime())
            {
                divNewRate.Visible = true;
                txtNewRate.Text = _carturboengine.FInfo1.ToString();
                rdoEffectivity.SelectedValue = "Yes";
            }
            else
            {
                divNewRate.Visible = false;
                rdoEffectivity.SelectedValue = "No";
            }

            usrEffectivityDate.Day = _carturboengine.DInfo1.Day;
            usrEffectivityDate.Month = _carturboengine.DInfo1.Month;
            usrEffectivityDate.Year = _carturboengine.DInfo1.Year;
        }
        else
        {
            txtRate.Text = "";
        }
        lblMessage.Text = "";
    }

    private Int64 CheckCarTurboEngine(Int64 ProviderCompanyId, bool IsTurbo)
    {
        CarTurboEngine _carturboengine = new CarTurboEngine();
        CarTurboEngineDB _carturboenginedb = new CarTurboEngineDB();
        Int64 _retValue = 0;
        _carturboengine = _carturboenginedb.GetCarTurboEngine(ProviderCompanyId, IsTurbo);
        if (_carturboengine != null)
        {
            _retValue = _carturboengine.ProviderCarTurboEngineId;
        }

        return _retValue;
    }

    protected void ddlTurbo_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlTurbo.Text != "-1")
        {
            ShowCarTurboEngine(Convert.ToInt64(Session[General.ProviderCompanyId].ToString()), Convert.ToBoolean(ddlTurbo.Text));
        }
        else
        {
            txtRate.Text = "";
            lblMessage.Text = "";
        }
    }
    protected void ibtBack_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("~/Provider/CarInsurance/TableMatching.aspx");
    }

    public void EffectiveWhen(object sender, EventArgs e)
    {
        if (rdoEffectivity.SelectedValue == "Yes")
        {
            divNewRate.Visible = true;
        }
        else
        {
            divNewRate.Visible = false;
        }

        usrEffectivityDate.Day = CE.Global.General.GetUTCDateTime().Day;
        usrEffectivityDate.Month = CE.Global.General.GetUTCDateTime().Month;
        usrEffectivityDate.Year = CE.Global.General.GetUTCDateTime().Year;
    }

    protected bool CheckAuthCode(string strAuthCode, int intAuthCodeType)
    {
        bool _retValue = false;
        ProviderAuthCode _authcode = new ProviderAuthCode();
        ProviderAuthCodeDB _authcodedb = new ProviderAuthCodeDB();
        _authcode = _authcodedb.GetProviderAuthCode(Convert.ToInt64(Session[General.ProviderCompanyId].ToString()), intAuthCodeType, strAuthCode);
        if (_authcode != null)
        {
            _retValue = true;
        }

        return _retValue;
    }

    protected Int64 InsertProviderAuthTransaction(Int64 TransactionId, string TransactionCode, int TransactionType,
        int AuthCodeType, string AuthCode, Int64 ProviderCompanyId, Int64 RefCEUserId)
    {
        ProviderAuthTransaction _authtrans = new ProviderAuthTransaction();
        ProviderAuthTransactionDB _authtransdb = new ProviderAuthTransactionDB();

        _authtrans.TransactionId = TransactionId;
        _authtrans.TransactionCode = TransactionCode;
        _authtrans.TransactionType = TransactionType;
        _authtrans.AuthCode = AuthCode;
        _authtrans.AuthCodeType = AuthCodeType;
        _authtrans.ProviderCompanyId = ProviderCompanyId;
        _authtrans.RefCEUserID = RefCEUserId;

        _authtrans.AuthTransId = _authtransdb.Insert(_authtrans);
        return _authtrans.AuthTransId;
    }

    private void CheckMainPageAccess()
    {
        Int64 PageAccess = General.GetProviderMainPageAccess(Convert.ToInt64(Session[General.ProviderCompanyId].ToString()),
                                                        Convert.ToInt64(Session[General.UserId].ToString()), PageName);
        if (PageAccess == 0)
        {
            //Response.Redirect("~/Provider/UserMessage.aspx?errorcode=2000");
            Response.Redirect("~/Provider/UserMessage.aspx?mt=UM&ec=2000");
        }
    }

    private void CheckSubPageAccess()
    {
        Int64 PageAccess = General.GetProviderSubPageAccess(Convert.ToInt64(Session[General.ProviderCompanyId].ToString()),
                                                        Convert.ToInt64(Session[General.UserId].ToString()), SubPageName);
        if (PageAccess == 0)
        {
            //Response.Redirect("~/Provider/UserMessage.aspx?errorcode=2000");
            Response.Redirect("~/Provider/UserMessage.aspx?mt=UM&ec=2000");
        }
        else if (PageAccess == 100)
        {
            ibtSubmit.Enabled = false;
        }
    }


}
